package com.apps.sdses.flink141.test;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Iterator;
import java.util.regex.Pattern;

/**
 * @author jiwei
 * @description
 * @date 2024/1/4 17:13
 */
public class TopicsPatternConverter {
    public static void main(String[] args) {
        String jsonString = "{\"topics\":[\"abc\",\"abc*\",\"123*\"]}";

        try {
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode objectNode = objectMapper.readTree(jsonString);

            JsonNode topicsNode = objectNode.get("topics");

            // 将 JsonNode 中的列表项合并为一个正则表达式模式字符串
            StringBuilder patternBuilder = new StringBuilder();
            if (topicsNode != null && topicsNode.isArray()) {
                Iterator<JsonNode> elements = topicsNode.elements();
                while (elements.hasNext()) {
                    JsonNode element = elements.next();
                    patternBuilder.append(element.asText());
                    if (elements.hasNext()) {
                        patternBuilder.append("|"); // 添加 | 分隔符
                    }
                }
            }

            // 创建 Pattern 对象
            Pattern topicsPattern = Pattern.compile(patternBuilder.toString());

            // 输出合并后的正则表达式模式字符串
            System.out.println("Merged Pattern: " + topicsPattern.pattern());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
